#include <iostream>
using namespace std;
#include <signal.h>
#include <unistd.h>
#define BLOCK_SIGNAL 2
#define MAX_SIGNUMS 31


static void show_pending(sigset_t &pending)
{
    for(int signo = MAX_SIGNUMS;signo>=1;--signo)
    {
        if(sigismember(&pending,signo))
        {
            cout<<"1";
        }
        else cout<<"0";
    }
    cout<<endl;
} 

static void myhandler(int sig)
{
    cout<<sig<<"号信号已经被递达"<<endl;
}

int main()
{
    signal(BLOCK_SIGNAL,myhandler);
    //1、尝试屏蔽你的信号
    sigset_t block,oblock,pending;
    //初始化
    sigemptyset(&block);
    sigemptyset(&oblock);
    sigaddset(&block,BLOCK_SIGNAL);
    //开始屏蔽
    sigprocmask(SIG_SETMASK, &block,&oblock);
    int cnt = 10;
    //2、尝试进行遍历打印所有的pending信号集
    while(true)
    {
        sigemptyset(&pending);
        sigpending(&pending);
        //打印pending
        show_pending(pending);
        sleep(1);
        if(cnt-- == 0)
        {
            //恢复对信号的屏蔽
            cout<<"已恢复"<<endl;
            sigprocmask(SIG_SETMASK,&oblock,&block);
            cnt = 10;
            sigprocmask(SIG_SETMASK, &block,&oblock);
        }
    }
    return 0;
}